package com.vue.cn.utils.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Component
public class MailUtils {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${mail.from}")
    private String from;

    @Value("${mail.subjectCode}")
    private String subjectCode;

    @Value("${mail.subjectActive}")
    private String subjectActive;

    @Async
    public void sendCode(String email,String code){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(email);
        message.setSubject(subjectCode);
        message.setText("邮箱验证码为  "+ code+"请尽快使用，5分钟后失效");
        javaMailSender.send(message);
    }

    @Async
    public void sendActive(String email,String htmlStr) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, "utf-8");
        messageHelper.setFrom(from);
        messageHelper.setTo(email);
        messageHelper.setSubject(subjectActive);
        messageHelper.setText(htmlStr, true);
        javaMailSender.send(mimeMessage);

    }

}
